import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        addressList: {}, //收货地址的数据
        shoppingList: {}, //商品详情
        goodsIntegralId: "", //商品的id
        count: 0,
        deduction: 0, //扣除的积分
        totalIntegral: 0,
        postage: 0, //配送费
        totalPrices: "", //总价
        addressId: "", //地址的id
        pocketPrice: 0, //实付金额
        orderNumber: "", //订单编号
        checked: true,
        show: false,
        radio: "1",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            goodsIntegralId: wx.getStorageSync("id"),
        });

        this.shoppingPost();
        this.myUserInfo();
    },
    //商品详情数据
    async shoppingPost() {
        let res = await request({
            url: "GoodsIntegral/findByGoodsIntegralId",
            data: { goodsIntegralId: this.data.goodsIntegralId },
        });
        console.log(res, "888888888");

        if (res.code == 200) {
            this.setData({
                shoppingList: res.data,
                totalPrices: res.data.integral * this.data.count, //赋值总价用于获取可用优惠卷
            });
            this.postagePost(); //计算配送费
        }
    },
    // 点击地址区域的按钮
    administration() {
        wx.navigateTo({
            url: "../Integral_Address/Integral_Address",
        });
    },

    //计算配送费
    async postagePost() {
        let res = await request({
            url: "Address/shippingFee",
            data: {
                addressId: this.data.addressList.addressId, //地址的id
            },
        });
        console.log(res);

        if (res.code == 200) {
            // 存储商品的配送费
            wx.setStorageSync("packingFee", res.data);
            this.setData({
                postage: res.data, //赋值配送费
                pocketPrice: res.data,
            });
        }
    },
    //用户信息
    async myUserInfo() {
        let res = await request({
            url: "User/findById",
        });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                totalIntegral: res.data.integral,
            });
        }
    },
    //获取用户的信息,比较用户余额
    async userInfoPost() {
        let res = await request({
            url: "User/findById",
        });
        if (this.data.pocketPrice > res.data.money) {
            wx.showToast({
                title: "用户余额不足,请充值!",
                icon: "none",
                duration: 2000,
            });
            console.log("没有执行");
            return;
        } else {
            console.log(res, "执行了");
            this.balancePayment(); //余额支付
        }
    },
    //提交订单按钮
    async goOkOrder() {
        if (this.data.addressList == "") {
            wx.showToast({
                title: "请选择收货地址",
                icon: "none",
                image: "",
                duration: 1500,
            });
            return;
        }
        this.setData({
            show: true,
        });
    },
    //支付方式的change事件
    onChange(e) {
        console.log(e);
        this.setData({
            radio: e.detail,
        });
    },
    //关闭弹框
    onClose() {
        this.setData({
            show: false,
        });
    },
    //弹框确认按钮
    async getUserInfo() {
        let res = await request({
            url: "BlanketOrderIntegral/addOrder",
            data: {
                goodId: wx.getStorageSync("id"),
                count: wx.getStorageSync("countCredit"),
                addressId: this.data.addressId,
            },
        });
        console.log(res);

        if (res.code == 200) {
            this.setData({
                orderNumber: res.data.orderNumber,
            });
            if (this.data.radio == "1") {
                this.paymentPost();
            } else {
                this.userInfoPost();
            }
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
                image: "",
                duration: 1500,
            });
            return;
        }
    },
    //余额支付
    async balancePayment() {
        let res = await request({
            url: "BlanketOrderIntegral/balancePay",
            data: { orderNumber: this.data.orderNumber },
        });
        console.log(res, "余额支付");
        if (res.code == 200) {
            setTimeout(function() {
                wx.switchTab({
                    url: "../myName/index",
                });
            }, 2000);
            wx.showToast({
                title: "支付成功",
                image: "",
                duration: 1500,
            });
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
            });
        }
    },
    //支付接口
    async paymentPost() {
        let res = await request({
            url: "pay/wx/toPayIntegral",
            data: { orderNumber: this.data.orderNumber },
        });
        console.log(res);

        if (res.code == 200) {
            let aa = res.data.timeStamp + "";
            console.log(aa);
            wx.requestPayment({
                timeStamp: aa, //时间戳
                nonceStr: res.data.nonceStr, //随机字符串
                package: res.data.package, //统一下单接口返回的 prepay_id 参数值
                signType: res.data.signType, //签名算法
                paySign: res.data.sign, //签名
                success: function(res) {
                    wx.showToast({
                        title: "支付成功",
                        duration: 1500,
                    });
                    setTimeout(() => {
                        wx.switchTab({
                            url: "../myName/index",
                        });
                    }, 2000);
                },
                fail: function(err) {
                    wx.showToast({
                        title: "支付失败",
                        icon: "none",
                        duration: 1500,
                    });
                    setTimeout(() => {
                        wx.switchTab({
                            url: "../myName/index",
                        });
                    }, 2000);
                },
            });
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let addressList = wx.getStorageSync("addressCredit");
        this.setData({
            addressList: addressList, //赋值收货地址的信息
            addressId: addressList.addressId, //赋值地址的id
            count: wx.getStorageSync("countCredit"),
        });
        if (wx.getStorageSync("addressCredit")) {
            this.setData({
                pocketPrice: wx.getStorageSync("packingFee"),
                postage: wx.getStorageSync("packingFee"),
            });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});